package com.sinopec.uhf.chengwei.settings

import com.rscja.deviceapi.RFIDWithUHFA8
import com.rscja.deviceapi.entity.AntennaState
import com.rscja.deviceapi.enums.AntennaEnum
import com.sinopec.iuhf.IUHFSettings
import kotlinx.coroutines.delay

/**
 *
 * 好像有点问题 这里的功率是指第一个天线功率
 *
 */
object Power : IUHFSettings.ASeting<Int> {
    override suspend fun get(): Int {
        val rfid =
            RFIDWithUHFA8.getInstance() ?: return 0
        val powers = rfid.antennaPower
        return powers?.getOrNull(0)?.power ?: 0

    }

    private val ants = listOf(
        AntennaEnum.ANT1,
//        AntennaEnum.ANT2,
//        AntennaEnum.ANT3,
//        AntennaEnum.ANT4,
//        AntennaEnum.ANT5,
//        AntennaEnum.ANT6,
//        AntennaEnum.ANT7,
//        AntennaEnum.ANT8,
    )

    private val antStates = ants.map {
        AntennaState(it,true)
    }

    override suspend fun set(t: Int): Result<*> {
        val rfid =
            RFIDWithUHFA8.getInstance() ?: return Result.failure<Unit>(Exception("无法连接UHF"))
        rfid.ant = antStates
        ants.forEach {
            rfid.setAntennaPower(it, t)
            delay(50)
        }
        return Result.success(Unit)

    }
}